package com.app.shop.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.app.library.base.BaseActivity;
import com.app.library.beans.UserBean;
import com.app.library.utils.StringUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.shop.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {


   private TextView tvTitle;

    private EditText inputMobile;

    private EditText inputpwd;

    private EditText inputpwdRepeat;

    private EditText inputName;

    private EditText inputNum;
    private TextView tvregister;
    private ImageView imgvreturn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        inputMobile= (EditText) findViewById(R.id.inputMobile);
        inputpwd= (EditText) findViewById(R.id.inputpwd);
        inputpwdRepeat= (EditText) findViewById(R.id.inputpwd_repeat);
        inputName= (EditText) findViewById(R.id.inputName);
        inputNum= (EditText) findViewById(R.id.inputNum);
        tvregister= (TextView) findViewById(R.id.tv_register);
        imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        tvregister.setOnClickListener(this::onClick);
        imgvreturn.setOnClickListener(this::onClick);
        tvTitle.setText("注册");
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.tv_register:
                String name = inputName.getText().toString();
                String mobile = inputMobile.getText().toString();
                String no = inputNum.getText().toString();
                String pwd = inputpwd.getText().toString();
                String pwd_2 = inputpwdRepeat.getText().toString();
                if (StringUtil.isEmpty(no) || StringUtil.isEmpty(name) || StringUtil.isEmpty(mobile) || StringUtil.isEmpty(pwd) || StringUtil.isEmpty(pwd_2)) {
                    ToastUtil.showToast(this, "请完善信息");
                    return;
                }

                if (!pwd.equals(pwd_2)) {
                    ToastUtil.showToast(this, "两次密码不一致");
                    return;
                }

                if (UserManager.isHaveUser(mobile)) {
                    ToastUtil.showToast(this, "已存在该用户");
                    return;
                }


                UserBean userBean = new UserBean();
                userBean.setMobile(mobile);
                userBean.setUser_id(mobile); //登录名
                userBean.setName(name);
                userBean.setPassword(pwd);
                userBean.student_num = no;
                userBean.save();
                ToastUtil.showToast(this, "注册成功");
                onBackPressed();

                break;
        }
    }
}
